#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <string.h>
#include "protocol.h"
#include "utility.h"
#include "asa.h"


int main()
{
	char cmd[20];
	char *ip_str = NULL;
	unsigned int ip_attack;
	asa * snifox = new asa;
	list<unsigned int> * host;
	list<unsigned int>::iterator itor;
	int ret;

	ret = snifox->init();
	if(ret)
		return 1;
	ret = snifox->run();
	if(ret)
		return 1;
	while(1)
	{
		printf("\n[root@dora]#");
		fgets(cmd,20,stdin);
		if(cmd[strlen(cmd) - 1] == '\n')
			cmd[strlen(cmd) - 1] = '\0';
		if(!strcmp(cmd, "quit") || !strcmp(cmd, "q"))
			break;
		if(!strcmp(cmd, "list") || !strcmp(cmd,"l"))
		{
			host = snifox->query_online_host();
			if(host->size())
			{
				for(itor=host->begin(); itor != host->end(); ++itor)
				{
					printf("\n%s",inet_ntoa(*(struct in_addr *)&(*itor)));
				}				
			}
			else
			{
				printf("no online host");
			}
			delete host;
		}
		else if(!strncmp(cmd, "attack ",7) || !strncmp(cmd, "a ",2))
		{
			strtok(cmd," ");
			ip_str = strtok(NULL," ");
			if(ip_str)
			{
				if(inet_aton(ip_str,(struct in_addr *)&ip_attack))
				{
					printf("attack ip: %s",ip_str);
					snifox->attack(ip_attack);	
				}
				else
				{
					printf("attack command need a ip as parameter");
				}
			}
			else
			{
				printf("attack command need a ip as parameter");
			}
		}	

	}

	ret = snifox->stop();

	delete snifox;

/*
	printf("uid = %d\neuid = %d\n",getuid(),geteuid());
	ret = get_dev_ip("eth0", &ip);
	printf("ret = %d\t ip: %d.%d.%d.%d\n",ret,ip.ip[0],ip.ip[1],ip.ip[2],ip.ip[3]);
	ret = get_gateway_ip(&ip);
	printf("ret = %d\t ip: %d.%d.%d.%d\n",ret,ip.ip[0],ip.ip[1],ip.ip[2],ip.ip[3]);

	get_dev_mac("eth0", &mac);
*/
	return 0;
}
